package cn.bm.web.services;

import cn.bm.web.db.DBConfig;

import java.sql.*;

import static java.sql.JDBCType.NULL;

/**
 * 操作数据库
 */
public class UserServices {
    public static Boolean login(String username, String password) {
        Connection conn = DBConfig.getConnection();
        try {
            String sql = "select * from 用户表 where user = ? and password = ?";
            PreparedStatement p = conn.prepareStatement(sql);
            p.setString(1, username);
            p.setString(2, password);
            if (p.executeQuery().next()) {
                return true;
            } else {
                return false;
            }

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
            try {
                conn.close();
            } catch (SQLException ex) {
                //
            }
        }

        return false;
    }

    public static Boolean register(String username, String password) {
        Connection conn = DBConfig.getConnection();
        try {
            String sql = "insert into 用户表 values(?,?)";
            PreparedStatement p = conn.prepareStatement(sql);
            p.setString(1, username);
            p.setString(2, password);
            p.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
            try {
                conn.close();
            } catch (SQLException ex) {
                //
            }
        }

        return false;
    /*public static void main(String[] args) {
        register("lys1","123456");
    }*/
    }
}
